/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : club_view.js                                 
*  Description  : 개별클럽 일반게시판 상세보기
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-23  이윤노    Created.  
*  2012-05-00  이윤노    Complete.  
*******************************************************************************/

jQuery(function($){
	// 상단 타이틀
	function clsClubTitle()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsClubTitle.prototype = {
		_init: function(){
			var $layer = $("div#container>section#main_content>div.golf_board article.depth2_tab");
			
			this._HTMLDom = {
				"$layer": $layer
				, "$tabs": $layer.find("li")
			}
			
			$layer.data("idx", null); // 인덱스로 관리
			
			this.display();
		}
		, setIdx: function(val){
			
			var $layer = this._HTMLDom["$layer"];
			
			if(val == $layer.data("idx")) return false;
			
			$layer.data("idx", val);
			
			this.display();
			return true;
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"], $tabs = this._HTMLDom["$tabs"];
			var idx = $layer.data("idx");
			
			// 현재 인덱싱된 타이틀만 on설정
			$tabs.find("img").each(function(n){
				var imgSrc = $(this).attr("src");
				var szImgName = imgSrc.replace(/(.*)\/(.*).gif/g, function(){
					return arguments[1] + "/" + arguments[2].replace("_on", "");
				});
				
				$(this).attr("src", szImgName + ((idx == n) ? "_on":"") + ".gif");
			});
		}
		// 탭 버튼
		, $getsBtnTab: function(){
			var $layer = this._HTMLDom["$layer"], $tabs = this._HTMLDom["$tabs"];
			
			return $tabs.find("a");
		}
	}
	
	//상단메뉴
	function clsTopMenuInfo(){
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsTopMenuInfo.prototype = {
		_init: function(){
			var $layer = $("div#container nav ul.self_club_gnb");			
			this._HTMLDom = {
				"$layer":$layer
			}			
		}
		, getBtnClubHome: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:first");
		}
		, getBtnClubField: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(1)");
		}
		, getBtnClubBoard: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(2)");
		}
		, getBtnClubPic: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(3)");
		}
		, getBtnClubAdmin: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(4)");
		}
	}
	
	// 클럽게시판상세
	function clsClubInfo()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsClubInfo.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.board_view")
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("dl.view_tit dt").contents().eq(0).remove().end().end().prepend(this.infoData["제목"]);
			$layer.find("dl.view_tit dt span").html("(" + this.infoData["댓글수"] + ")");
			$layer.find("dl.view_tit dd span:eq(0) img").attr("src", this.infoData["작성자"]["이미지"]);
			$layer.find("dl.view_tit dd span:eq(1)").html(this.infoData["작성자"]["닉네임"]);
			$layer.find("table thead th:eq(1) span:eq(1)").contents().eq(0).remove().end().end().prepend(this.infoData["작성일"])
			$layer.find("table thead th:eq(1) span:eq(1) span").html(this.infoData["작성시"]);
			$layer.find("table thead th:eq(2) span:eq(1)").html(this.infoData["조회수"]);
			$layer.find("table tbody td").html(this.infoData["내용"]);
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("dl:first, table:first").hide();
			if($layer.find("div#tmpSpin").length == 0){
				$layer.prepend("<div id='tmpSpin' style='position:relative; width:600px; height:100px;'></div>");
			}
			$layer.find("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("div#tmpSpin").hide().spin(false);
			$layer.find("dl, table").show();
		}
	}
	
	function clsClubAdminInfo(){
		this._HTMLDom = null;
		this.adminData = null;	
		
		this._init();
	}
	
	clsClubAdminInfo.prototype = {		
		_init: function(){
				
				var $layer = $("div#container section#main_content div.golf_board div.club_creat");
				this._HTMLDom = {
					"$layer": $layer
				}
				
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];

			$layer.hide().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.spin(false).show();
		}
		, setData: function(thisData){
			this.adminData = thisData;
			this.display();	
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("td:eq(0)").html(this.adminData["동호회명"]);
			$layer.find("td:eq(1)").html(this.adminData["동호회소개"]);
			$layer.find("td:eq(2)").html(this.adminData["승인여부"]);
			$layer.find("td:eq(3)").html(this.adminData["카테고리"]);
			$layer.find("td:eq(4) img").attr("src", this.adminData["로고이미지"]);
			$layer.find("td:eq(5) img").attr("src", this.adminData["배너대형"]);
			$layer.find("td:eq(6) img").attr("src", this.adminData["배너소형"]);
		}
	}
	
	function clsComm()
	{
		this.clubRequest = null;
		
		this.clubData = null;
		this.clubAdminData = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.clubRequest = {
				"클럽키":null		
			}
			
			this.clubData = {
				"제목": null
				, "댓글수": null
				, "작성자": {
						"아이디": null
						, "이미지": null
						, "닉네임": null
					}
				, "작성일": null
				, "작성시": null
				, "조회수": null
				, "내용": null
			}
			
			this.clubAdminData = {
				"동호회명": null
				,"동호회소개" : null
				,"승인여부" : null
				,"카테고리" : null
				,"로고이미지" : null
				,"배너대형" : null
				,"배너소형" : null					
			}
		}
		,getClubAdminInfo: function(){
			var thisClass = this;
			var board_seq = (thisClass.objComm.getClubRequest("클럽키"));
			
			//ajax
			setTimeout(function(){
				thisClass.clubAdminData = {
					"동호회명": "나는 골프다-----"
					,"동호회소개" : "나는 골프다를 시청하는 사람들의 모임 입니다-----"
					,"승인여부" : "누구나 가입가능-----"
					,"카테고리" : "카테고리 1-----"
					,"로고이미지" : "../images/board/banner_70_70.gif"
					,"배너대형" : "../images/board/banner_400_70.gif"
					,"배너소형" : "http://192.168.0.82:8080/sky72web/images/board/banner_225_70.gif"					
				}
				
				$(thisClass).trigger("evtFinishClubAdminInfo");
			}, 500);
		}
		, getClubAdminData: function(){
			return this.clubAdminData;	
		}
		
		, getClubInfo: function(){
			var thisClass = this;
			var board_seq = (thisClass.objComm.getClubRequest("클럽키"));
			
			//ajax
			setTimeout(function(){
				thisClass.clubData = {
					"제목": "제목이 노출됩니다. 제목이 노출됩니다. 제목이 노출됩니다. 제목이 노출됩니다. 제목이 노출됩니다~람쥐~."
					, "댓글수": 2
					, "작성자": {
						"아이디": "sky72"
						, "이미지": "../images/board/default_50_50.gif"
						, "닉네임": "오스케 짱말짱짱말짱"
					}
					, "작성일": "2012-03-03"
					, "작성시": "17:35"
					, "조회수": 256
					, "글권한": "Y"
					, "내용": "<img  src='../images/board/del.gif' alt=''/><br/><br/>코스 위에 정지하여 있는 흰 볼을 지팡이 모양의 클럽으로 잇달아 쳐서 정해진 홀(球孔)에 넣어 그때까지 소요된 타수(打數)의 많고 적음으로 우열을 겨루는 경기이다. 골프코스는 들판·구릉·산림 등 66만∼100만㎡의 넓은 지역을 이용하여 정형(整形)되어 있고, 해변에 만들어지는 시사이드 코스와 내륙에 만들어지는 인랜드 코스가 있다."
				}
				
				$(thisClass).trigger("evtFinishClubInfo");
			}, 500);
			
		}
		, getClubData: function(){
			return this.clubData;
		}
		, setClubRequest: function(key, val){
			this.clubRequest[key] = val;
		}
		, getClubRequest: function(key){
			return 	this.clubRequest[key];
		}
		, clubMemeberCheck: function(){
			var thisClass = this;
			var club_seq = (thisClass.objComm.getClubRequest("클럽키"));
			var ret = null;
			
			//ajax
			setTimeout(function(){
				ret = {
					"삭제가능": "Y"
					, "회원수": 0	
				}
				
				$(thisClass).trigger("evtFinishMemberCheck", [ret]);
			}, 500);
		}
		, clubClose: function(){
			var thisClass = this;
			var club_seq = (thisClass.objComm.getClubRequest("클럽키"));
			var ret = null;
			
			//ajax
			setTimeout(function(){
				ret = {"응답코드": "0000"
					, "메세지": "정상"
				}
				
				$(thisClass).trigger("evtFinishClubClose", [ret]);
			}, 500);
		}
	}
	
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objClubTitle = null;
		this.objClubInfo = null;
		this.objTopMenuInfo = null;
		
		this.objClubAdminInfo = null;
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			
			this.objTopMenuInfo = new clsTopMenuInfo();
			this.objClubTitle = new clsClubTitle();
			this.objClubInfo = new clsClubInfo();
			this.objClubAdminInfo = new clsClubAdminInfo();
			
			//동호회정보/회원관리/탈퇴회원/가입승인/정기라운딩 탭설정
			this.objClubTitle.setIdx((document.objUtil.getParam("q_tab")?document.objUtil.getParam("q_tab"):0));
			
			this.objComm.setClubRequest("클럽키", document.objUtil.getParam("club_seq"));			
			
			this._bind();
			
			this.objComm.getClubInfo();
			this.objClubAdminInfo.spinStart();
			
		}
		, _bind: function(){
			var thisClass = this;

			//<--navi 페이지 설정 시작
			//홈
			this.objTopMenuInfo.getBtnClubHome().click(function(event){
				event.preventDefault();						
				document.location.href = "home.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키"));
			});
			//동호회필드
			this.objTopMenuInfo.getBtnClubField().click(function(event){
				event.preventDefault();						
				document.location.href = "club_field.html?club_seq=" +(thisClass.objComm.getClubRequest("클럽키"));
			});
			//일반게시판
			this.objTopMenuInfo.getBtnClubBoard().click(function(event){
				event.preventDefault();						
				document.location.href = "club_list.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키"));
			});
			//사진게시판
			this.objTopMenuInfo.getBtnClubPic().click(function(event){
				event.preventDefault();						
				document.location.href = "club_picture_list.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키"));
			});
			//관리하기
			this.objTopMenuInfo.getBtnClubAdmin().click(function(event){
				event.preventDefault();						
				document.location.href = "club_admin_info.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키"));				
			});
			//navi 페이지 설정 시작 -->
			
			//동호회정보/회원관리/탈퇴회원/가입승인/정기라운딩 탭 이벤트설정
			this.objClubTitle.$getsBtnTab().click(function(event){
				event.preventDefault();
				var idx = thisClass.objClubTitle.$getsBtnTab().index($(this));
				switch(idx){
					case 0 : document.location.href = "club_admin_info.html?club_seq=" + thisClass.objComm.getClubRequest("클럽키") + "&q_tab=" + idx;	break;
					case 1 : document.location.href = "club_admin_mem.html?club_seq=" + thisClass.objComm.getClubRequest("클럽키") + "&q_tab=" + idx;	break;
					case 2 : document.location.href = "club_admin_out.html?club_seq=" + thisClass.objComm.getClubRequest("클럽키") + "&q_tab=" + idx;	break;
					case 3 : document.location.href = "club_admin_join.html?club_seq=" + thisClass.objComm.getClubRequest("클럽키") + "&q_tab=" + idx;	break;
					case 4 : document.location.href = "club_admin_rd.html?club_seq=" + thisClass.objComm.getClubRequest("클럽키") + "&q_tab=" + idx;	break;
				}
				
			});
			//동호회정보 완료이벤트
			$(this.objComm).bind("evtFinishClubInfo", function(){
				thisClass.objClubAdminInfo.spinStart();
				thisClass.objComm.getClubAdminInfo();
			});
			//동호회상세정보 완료이벤트
			$(this.objComm).bind("evtFinishClubAdminInfo", function(){				
				var thisData = thisClass.objComm.getClubAdminData();
				
				thisClass.objClubAdminInfo.setData(thisData);
				thisClass.objClubAdminInfo.spinEnd();
			});
			//동호회폐쇄-가입회원조회 완료이벤트
			$(this.objComm).bind("evtFinishMemberCheck", function(event, ret){
				var club_name = thisClass.objClubAdminInfo.adminData["동호회명"];
			
				if(ret["회원수"]) return alert("동호회 폐쇄는 가입회원이 없어야 가능합니다.");
				
				if(confirm("[" + club_name + "]을 정말로 폐쇄하시겠습니까?")) thisClass.objComm.clubClose();
					
			});
			//동호회폐쇄-폐쇄 완료이벤트			
			$(this.objComm).bind("evtFinishClubClose", function(event, ret){
				var club_name = thisClass.objClubAdminInfo.adminData["동호회명"];
				
				if(ret["응답코드"]=="0000") {
					alert("[" + club_name + "]가 폐쇄되었습니다.");
					if(opener) {
						opener.location.href = "../index/index.html";
						this.close();
					}else{
						document.location.href = "../index/index.html";						
					}
					
				}else {
					alert("오류="+ ret["메세지"]);
				}
			});
			//동호회폐쇄 버튼 이벤트
			$("div#container section#main_content div.golf_board p.btn_area_box a:eq(0)").click(function(event){
				event.preventDefault();
				thisClass.objComm.clubMemeberCheck();
			});
			//동호회폐쇄 수정 이벤트
			$("div#container section#main_content div.golf_board p.btn_area_box a:eq(1)").click(function(event){
				event.preventDefault();
				document.location.href = "club_admin_write.html?club_seq=" + thisClass.objComm.getClubRequest("클럽키");
			});
		}
	}
	
	document.objControl = new clsControl();
});